package com.gack.business.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gack.business.dao.StoreFacityAndServiceNewDao;
import com.gack.business.model.StoreSupportFacity;
import com.gack.business.model.StoreSupportService;
import com.gack.business.repository.StoreSupportFacityIconRepository;
import com.gack.business.repository.StoreSupportFacityRepository;
import com.gack.business.repository.StoreSupportServiceIconRepository;
import com.gack.business.repository.StoreSupportServiceRepository;

/**
 * 
* @ClassName: StoreFacityAndServiceNewService 
* @Description: TODO(新的门店配套设施 配套服务业务类) 
* @author (ZhangXinYu)  
* @date 2018年9月7日 下午2:31:59 
* @version V1.0
 */
@Service
public class StoreSupportFacityAndServiceNewService implements StoreSupportFacityAndServiceNewServiceInterface{
	@Autowired
	private StoreFacityAndServiceNewDao newDao;
	@Autowired
	private StoreSupportFacityRepository facityRepository;
	@Autowired
	private StoreSupportServiceRepository serviceRepository;
	@Autowired
	private StoreSupportFacityIconRepository facityIconRepository;
	@Autowired
	private StoreSupportServiceIconRepository serviceIconRepository;
	@Autowired
	private BackLogService backLog;
	
	@Override
	public Map<String, Object> editFacity(String backId, String id, String names, String icons, int useTemplate) {
		Map<String, Object> map = new HashMap<>();
		List<String> nameList = new ArrayList<>(Arrays.asList(names.split("@;-;@")));
		List<String> iconList = new ArrayList<>(Arrays.asList(icons.split("@;-;@")));
		if(nameList.size() == 1 && (names == null || names.equals(""))){
			map.put("key", "error");
			map.put("msg", "名称不可为空");
			return map;
		}
		if(iconList.size() == 1 && (icons == null || icons.equals(""))){
			map.put("key", "error");
			map.put("msg", "icon不可为空");
			return map;
		}
		if(nameList.size() == 1 && newDao.hasFacity(id, names)){
			map.put("key", "error");
			map.put("msg", "名称已存在");
			return map;
		}
		if(useTemplate == 1 && facityIconRepository.findOne(icons) == null){
			// 使用图标模板 但是图标模板不存在
			map.put("key", "error");
			map.put("msg", "icon不存在");
			return map;
		}
		List<StoreSupportFacity> facitys = new ArrayList<>();
		String logStr = null;
		// 添加一个或多个
		if(id == null || id.equals("")){
			Date now = new Date();
			
			// 查询存在的门店配套设施
			List<String> existsFacityNames = newDao.findExistsFacityName(nameList);
			// 生成实例  排除已存在的配套设施名称
			int length = nameList.size() <= iconList.size() ? nameList.size() : iconList.size();
			for(int i = 0; i< length; i++) {
				if(existsFacityNames != null && existsFacityNames.contains(nameList.get(i))){
					// 该名称已存在
					continue;
				}
				StoreSupportFacity facity = new StoreSupportFacity();
				facity.setCreatetime(now);
				facity.setIsUse(0);
				facity.setName(nameList.get(i));
				facity.setIcon(iconList.get(i));
				facity.setUse_template(useTemplate);
				facitys.add(facity);
			}
			
			logStr = "添加门店配套设施成功>> name : " + names;
		}else{
			StoreSupportFacity facity = facityRepository.findOne(id);
			if(facity == null){
				map.put("key", "error");
				map.put("msg", "门店配套设施不存在");
				return map;
			}
			facity.setName(names);
			facity.setIcon(icons);
			facity.setUse_template(useTemplate);
			facitys.add(facity);
			logStr = "编辑门店配套设施成功>> name : " + names;
		}
		facitys = facityRepository.save(facitys);
		if(facitys == null || facitys.size() == 0){
			map.put("key", "error");
			map.put("msg", "操作失败");
			return map;
		}
		// 操作记录
		backLog.addBackLog(backId, logStr);
		map.put("key", "success");
		map.put("msg", "操作成功");
		return map;
	}
	
	@Override
	public Map<String, Object> deleteFacity(String backId, String id) {
		Map<String, Object> map = new HashMap<>();
		if(id == null || id.equals("")) {
			map.put("key", "error");
			map.put("msg", "id不可为空");
			return map;
		}
		StoreSupportFacity facity = facityRepository.findOne(id);
		if(facity == null || facity.getIsUse() == 2) {
			map.put("key", "error");
			map.put("msg", "门店配套设施不存在");
			return map;
		}
		if(facity.getIsUse() == 1){
			map.put("key", "error");
			map.put("msg", "门店配套设施使用中");
			return map;
		}
		facity.setIsUse(2);
		facity = facityRepository.save(facity);
		if(facity == null) {
			map.put("key", "error");
			map.put("msg", "操作失败");
			return map;
		}
		// 操作日志
		String logStr = "删除门店配套设施成功>> id: " + id;
		backLog.addBackLog(backId, logStr);
		map.put("key", "success");
		map.put("msg", "操作成功");
		return map;
	}
	
	/**
	 * 使用图标模板>> 添加一个   
	 * 不适用图标模板>> 添加一个或多个 
	 */
	@Override
	public Map<String, Object> editService(String backId, String id, String names, String icons, int useTemplate) {
		Map<String, Object> map = new HashMap<>();
		List<String> nameList = new ArrayList<>(Arrays.asList(names.split("@;-;@")));
		List<String> iconList = new ArrayList<>(Arrays.asList(icons.split("@;-;@")));
		if(nameList.size() == 1 && (names == null || names.equals(""))) {
			map.put("key", "error");
			map.put("msg", "名称不可为空");
			return map;
		}
		if(iconList.size() == 1 && (icons == null || icons.equals(""))) {
			map.put("key", "error");
			map.put("msg", "icon不可为空");
			return map;
		}
		if(nameList.size() == 1 && newDao.hasService(id, names)) {
			map.put("key", "error");
			map.put("msg", "名称已存在");
			return map;
		}
		if(useTemplate == 1 && serviceIconRepository.findOne(icons) == null) {
			// 使用图标模板 但是图标模板不存在
			map.put("key", "error");
			map.put("msg", "icon不存在");
			return map;
		}
		List<StoreSupportService> services = new ArrayList<>();
		String logStr = null;
		// 添加一个或多个
		if(id == null || id.equals("")) {
			Date now = new Date();
			
			// 查询已存在门店配套服务名称
			List<String> existsServiceNames = newDao.findExistsServiceName(nameList);
			// 生成实例  排除已存在配套服务
			int length = nameList.size() <= iconList.size() ? nameList.size() : iconList.size();
			for(int i = 0; i < length; i++) {
				if(existsServiceNames.contains(nameList.get(i))){
					// 该配套服务已存在  
					continue;
				}
				StoreSupportService service = new StoreSupportService();
				service.setCreatetime(now);
				service.setIsUse(0);
				service.setName(nameList.get(i));
				service.setIcon(iconList.get(i));
				service.setUse_template(useTemplate);
				services.add(service);
			}
			
			logStr = "添加门店配套服务成功>> name : " + nameList.toString();
		}else{
			StoreSupportService service = serviceRepository.findOne(id);
			if(service == null){
				map.put("key", "error");
				map.put("msg", "门店配套服务不存在");
				return map;
			}
			service.setName(names);
			service.setIcon(icons);
			service.setUse_template(useTemplate);
			services.add(service);
			logStr = "编辑门店配套服务成功>> name : " + nameList.toString();
		}
		
		services = serviceRepository.save(services);
		if(services == null || services.size() == 0) {
			map.put("key", "error");
			map.put("msg", "操作失败");
			return map;
		}
		// 操作记录
		backLog.addBackLog(backId, logStr);
		map.put("key", "success");
		map.put("msg", "操作成功");
		return map;
	}
	
	@Override
	public Map<String, Object> deleteService(String backId, String id) {
		Map<String, Object> map = new HashMap<>();
		if(id == null || id.equals("")) {
			map.put("key", "error");
			map.put("msg", "id不可为空");
			return map;
		}
		StoreSupportService service = serviceRepository.findOne(id);
		if(service == null || service.getIsUse() == 2) {
			map.put("key", "error");
			map.put("msg", "门店配套服务不存在");
			return map;
		}
		if(service.getIsUse() == 1){
			map.put("key", "error");
			map.put("msg", "门店配套服务使用中");
			return map;
		}
		service.setIsUse(2);
		service = serviceRepository.save(service);
		if(service == null) {
			map.put("key", "error");
			map.put("msg", "操作失败");
			return map;
		}
		// 操作日志
		String logStr = "删除门店配套服务成功>> id: " + id;
		backLog.addBackLog(backId, logStr);
		map.put("key", "success");
		map.put("msg", "操作成功");
		return map;
	}
	
	@Override
	public Map<String, Object> getAllFacity() {
		Map<String, Object> map = new HashMap<>();
		List<StoreSupportFacity> facitys = newDao.getAllFacity();
		map.put("key", "success");
		map.put("msg", "操作成功");
		map.put("facity", facitys);
		return map;
	}
	
	@Override
	public Map<String, Object> getAllService() {
		Map<String, Object> map = new HashMap<>();
		List<StoreSupportService> services = newDao.getAllService();
		map.put("key", "success");
		map.put("msg", "操作成功");
		map.put("service", services);
		return map;
	}
	
	@Override
	public Map<String, Object> getAllFacityAndService() {
		Map<String, Object> map = new HashMap<>();
		List<StoreSupportFacity> facitys = newDao.getAllFacity();
		List<StoreSupportService> services = newDao.getAllService();
		map.put("key", "success");
		map.put("msg", "操作成功");
		map.put("facity", facitys);
		map.put("service", services);
		return map;
	}

	@Override
	public Map<String, Object> searchSupportFacity(int searchType, String keyWord, String startTime, String endTime,
			int page, int size) {
		Map<String, Object> map = new HashMap<>();
		List<StoreSupportFacity> facities = newDao.searchSupportFacity(searchType, keyWord, startTime, endTime, page, size);
		long total = newDao.searchSupportFacityTotal(searchType, keyWord, startTime, endTime);
		map.put("key", "success");
		map.put("msg", "操作成功");
		map.put("facity", facities);
		map.put("total", total);
		return map;
	}

	@Override
	public Map<String, Object> searchSupportService(int searchType, String keyWord, String startTime, String endTime,
			int page, int size) {
		Map<String, Object> map = new HashMap<>();
		List<StoreSupportService> services = newDao.searchSupportService(searchType, keyWord, startTime, endTime, page, size);
		long total = newDao.searchSupportServiceTotal(searchType, keyWord, startTime, endTime);
		map.put("key", "success");
		map.put("msg", "操作成功");
		map.put("service", services);
		map.put("total", total);
		return map;
	}
}
